package com.MoveEat.activities.adapters;

import java.util.ArrayList;
import java.util.TreeSet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.MoveEat.R;
import com.MoveEat.items.DishDataItem;

public class MenuDataAdapter extends BaseAdapter
{
	private static final int	TYPE_ITEM		= 0;
	private static final int	TYPE_SEPARATOR	= 1;
	private static final int	TYPE_MAX_COUNT	= TYPE_SEPARATOR + 1;

	ArrayList<DishDataItem>		mList;
	Context						mContext;
	@SuppressWarnings("rawtypes")
	private TreeSet				mSeparatorsSet	= new TreeSet();

	@SuppressWarnings("unchecked")
	public MenuDataAdapter(ArrayList<DishDataItem> dishesList, Context context)
	{
		mList = dishesList;
		mContext = context;

		for (int i = 0; i < mList.size(); i++)
		{
			DishDataItem curr = mList.get(i);
			if (curr.isCat())
			{
				mSeparatorsSet.add(i);
			}
		}

	}

	@Override
	public int getViewTypeCount()
	{
		return TYPE_MAX_COUNT;
	}

	@Override
	public int getCount()
	{
		return mList.size();
	}

	@Override
	public int getItemViewType(int position)
	{
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public Object getItem(int pos)
	{

		return mList.get(pos);
	}

	@Override
	public long getItemId(int pos)
	{
		return pos;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parentView)
	{
		final ViewHolder holder;
		DishDataItem currObj = (DishDataItem) this.getItem(pos);
		int type = getItemViewType(pos);
		if (convertView == null)
		{
			holder = new ViewHolder();
			LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			switch (type)
			{
				case TYPE_SEPARATOR:
					convertView = vi.inflate(R.layout.menu_category_layout, null);
					holder.mCatText = (TextView) convertView.findViewById(R.id.cat_text);
					break;
				case TYPE_ITEM:
					convertView = vi.inflate(R.layout.menu_items, null);
					holder.mDishName = (TextView) convertView.findViewById(R.id.dish_name);
					holder.mDishDesc = (TextView) convertView.findViewById(R.id.dish_spec);
					holder.mDishPrice = (TextView) convertView.findViewById(R.id.dish_price);
					break;
			}
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (type==TYPE_SEPARATOR)
		{
			holder.mCatText.setText(currObj.getCatName());
		}
		else
		{
			if (currObj.getDISH_NAME() != null)
			{
				holder.mDishName.setText(currObj.getDISH_NAME());
			}
			if (currObj.getDISH_DESC() != null)
			{
				holder.mDishDesc.setText(currObj.getDISH_DESC());
			}
			if (currObj.getDISH_COST() != null)
			{
				holder.mDishPrice.setText(currObj.getDISH_COST());
			}
		}

		return convertView;
	}

	static class ViewHolder
	{
		TextView	mDishName;
		TextView	mDishPrice;
		TextView	mDishDesc;
		TextView	mCatText;
	}

}
